﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace GA_Scheduler
{
    public class CountDailyPairsPenalty : IPenalty
    {
        public int Score
        {
            get;
            set;
        }

        public string Name
        {
            get { return "CountDailyPairsPenalty"; }
            set { }
        }

        public int CalculatedScore
        {
            get;
            private set;
        }

        public bool Execute(Schedule sch)
        {
            CalculatedScore = 0;
            foreach (var gr in Configuration.Instance.Groups)
            {
                for (int i = 0; i < GA_Scheduler.Properties.Settings.Default.CountDays; i++)
                {
                    var dailyCourses = sch.Genes.Where(gen => gen.DayOfWeek == i && gen.Class.GroupClasses.FirstOrDefault(g => g.Groupd_ID == gr.Group_ID) != null)
                                       .Sum(g => g.Class.Duration);


                    if (dailyCourses == 0)
                    {
                        continue;
                    }

                    if (dailyCourses < GA_Scheduler.Properties.Settings.Default.MaxHoursDayily)
                    {
                        CalculatedScore += Score * (GA_Scheduler.Properties.Settings.Default.MaxHoursDayily - dailyCourses);
                    }
                    else if (dailyCourses > GA_Scheduler.Properties.Settings.Default.MaxHoursDayily)
                    {
                        CalculatedScore += Score * 3 * (dailyCourses - GA_Scheduler.Properties.Settings.Default.MaxHoursDayily);
                    }


                }
            }

            return CalculatedScore != 0;
        }
    }
}
